/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbAdpTanx-1.0.0		*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_01			*/
/*	File:		GlbAdpMsg.cpp			*/
/********************************************************/

#include "GlbPro.h"
#include "GlbTime.h"
#include "GlbAdpDef.h"

#define	GLB_AM_KVS			128

bool g_bPrint = false;

void GlbAdpMsgCore(GlbIpc::CGlbMsg* pCMsg)
{
	int iCount = 0;
	GlbNet::CGlbPro CPro;
	GlbCls::CGlbTime CTime;
	GLBADPMSG_S stMsg = {'\0', 0, {'\0'}};
	GlbNet::GLBPROKV_S stKv[GLB_AM_KVS] = {{'\0', 0, NULL}};

	while (true) {
		while ((pCMsg + iCount)->GlbMsgGet(&stMsg, sizeof(GLBADPMSG_S), GLB_IPC_NOWAIT) > 0) {
			if (true == g_bPrint) {
				CTime.GlbTimeOn();
				GLB_PRINT("IPCMSG[%d]:\n", iCount);
			}

			CPro.GlbProParse(stMsg.m_uszMsg, stMsg.m_iLength);
			GlbProKvInput(stKv, CPro.GlbProGetTotal(), CPro.GlbProGetBody(), CPro.GlbProGetLength());

			if (true == g_bPrint) {
				CPro.GlbProDisplay();
				GlbProKvDisplay(stKv, CPro.GlbProGetTotal());
				GLB_PRINT("Type: %d + Length: %d + Msg: ######\n", stMsg.m_ucType, stMsg.m_iLength);
			}
		
			if (true == g_bPrint) {
				CTime.GlbTimeOff();
				CTime.GlbTimeDisplay();
			}

			memset(&stMsg, '\0', sizeof(GLBADPMSG_S));
		}

		if (GLB_AD_MSG == ++ iCount) {
			usleep(10000);
			iCount = 0;
		}
	}

	return;
}

int main(int argc, char* argv[])
{
	int iCount = 0;
	GlbIpc::CGlbMsg* pCMsg = NULL;

	if (argc > 1) {
		g_bPrint = true;
	}

	pCMsg = new GlbIpc::CGlbMsg[GLB_AD_MSG];
	for (; iCount < GLB_AD_MSG; iCount ++) {
		if (GLB_SUCCESS != (pCMsg + iCount)->GlbIpcCreate((pCMsg + iCount)->GlbIpcKey(GLB_AD_PATH, GLB_AD_KEY + iCount))) {
			GLB_ERROR("Failed to GlbIpcCreate[%d]\n", iCount);
			return GLB_FAILURE;
		}
	}

	GlbAdpMsgCore(pCMsg);

	return GLB_SUCCESS;
}
